#include <cstdio>
#include <memory.h>

int a[ 1000002 ];
int n, r, b[ 501 ][ 501 ], c[ 501 ][ 501 ], maxn;

int lowbit( int t ) { return t & ( -t ); }

int sum( int x )
{
    int ret = 0;
    while ( x > 0 )
    {
        ret += a[ x ];
        x -= lowbit( x );
    }
    return ret;
}

void add( int x, int p )
{
    while ( x <= maxn )
    {
        a[ x ] += p;
        x += lowbit( x );
    }
}

void init( )
{
    int i, j;
    maxn = 1;
    for ( i = 0; i < n; i++ )
        for ( j = 0; j < n; j++ )
        {
            scanf("%d", &b[ i ][ j ]);
            b[ i ][ j ]++;
            if ( maxn < b[ i ][ j ] ) maxn = b[ i ][ j ];
        }
    memset( a, 0, sizeof( a ) );
}

int calc( )
{
    int left = 1, right = maxn, mid;
    while ( left < right )
    {
        mid = ( left + right ) >> 1;
        if ( sum( mid ) < ( r * 2 + 1 ) * ( r * 2 + 1 ) / 2 + 1 )
            left = mid + 1;
        else
            right = mid;
    }
    return left - 1;
}

void work( )
{
    int i, j, k;
    for ( i = 0; i < r * 2 + 1; i++ )
        for ( j = 0; j < r * 2 + 1; j++ )
            add( b[ i ][ j ], 1 );
    for ( i = 0; i < n - r * 2; i++ )
    {
        if ( i & 1 )
        {
            for ( j = 0; j < r * 2 + 1; j++ )
            {
                add( b[ i - 1 ][ n - j - 1 ], -1 );
                add( b[ i + r * 2 ][ n - j - 1 ], 1 );
            }
            c[ i ][ n - r * 2 - 1 ] = calc( );
            for ( j = 1; j < n - r * 2; j++ )
            {
                for ( k = 0; k < r * 2 + 1; k++ )
                {
                    add( b[ i + k ][ n - j ], -1 );
                    add( b[ i + k ][ n - j - r * 2 - 1 ], 1 );
                }
                c[ i ][ n - r * 2 - j - 1 ] = calc( );
            }
        }
        else
        {
            if ( i )
                for ( j = 0; j < r * 2 + 1; j++ )
                {
                    add( b[ i - 1 ][ j ], -1 );
                    add( b[ i + r * 2 ][ j ], 1 );
                }
            c[ i ][ 0 ] = calc( );
            for ( j = 1; j < n - r * 2; j++ )
            {
                for ( k = 0; k < r * 2 + 1; k++ )
                {
                    add( b[ i + k ][ j - 1 ], -1 );
                    add( b[ i + k ][ j + r * 2 ], 1 );
                }
                c[ i ][ j ] = calc( );
            }
        }
    }
    for ( i = 0; i < n - r * 2; i++ )
    {
        for ( j = 0; j < n - r * 2; j++ )
            printf("%d ", c[ i ][ j ]);
        printf("\n");
    }
}

int main( )
{
    while ( scanf("%d%d", &n, &r) )
    {
        if ( n == 0 && r == 0 ) break;
        init( );
        work( );
    }
    return 0;
}
